home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-11 | 484 b | 23 lines | [TEXT/ttxt] |
- ; factorial - the hard way
-
- ; if r0 = 1 then fact = 1 else fact := r0*r1
- begin_code
- copy #5=>r0 ; calculate fact (5)
- jump:subroutine ->fact
- halt
-
- fact
- compare #1=>r0 ; base case?
- jump:less_than generalcase
- copy #1=>r0
- copy #1=>r1 ; r1 serves as the multiplier
- jump endit
- generalcase
- subtract #1=>r0 ; n := n -1
- jump:subroutine ->fact ; fact (n-1)
- add #1=>r1 ; get n back
- multiply r1=>r0 ; n * fact(n-1)
- endit
- return
- end_code
- end